package com.unsiv.blplayer.util;

import java.io.File;
import java.io.FilenameFilter;

import android.os.Environment;

/**
 * 
 * @author fanzhen
 * @time   9:53:40 PM Oct 6, 2011
 * @email  fanzhen.meng@gmail.com
 */
public final class MyFileHelper {
	public static final String SDCARDPATH = Environment.getExternalStorageDirectory().getAbsolutePath();
	public static final String G_Mp3Folder = SDCARDPATH+"/blplayer/mp3/";// MP3
	public static final String G_BooksFolder = SDCARDPATH+"/blplayer/Books/";// 有声图书
	public static final String G_BIGMP3Folder = SDCARDPATH+"/blplayer/Reader/";// 大型MP3文件(原有声图书)
	public static final String G_RecordFolder = SDCARDPATH+"/blplayer/Record/";// 录音
	public static final String G_ReminderFolder = SDCARDPATH+"/blplayer/Reminder/";// 默认功能提示音
	public static final String G_RecordBufferFolder = SDCARDPATH+"/blplayer/RecordBuffer/";// 录音格
    public static final String G_TempPath = SDCARDPATH+"/blplayer/temp/";//临时目录
    
    /**
     * 初始化文件目录
     */
    public static void init(){
		File g_Mp3Folder = new File(G_Mp3Folder);
		File g_BIGMP3Folder = new File(G_BIGMP3Folder);
		File g_BooksFolder = new File(G_BooksFolder);
		File g_RecordBufferFolder = new File(G_RecordBufferFolder);
		File g_RecordFolder = new File(G_RecordFolder);
		File g_ReminderFolder = new File(G_ReminderFolder);
		File g_TempPath = new File(G_TempPath);
		if(!g_Mp3Folder.exists())g_Mp3Folder.mkdirs();
		if(!g_BIGMP3Folder.exists())g_BIGMP3Folder.mkdirs();
		if(!g_BooksFolder.exists())g_BooksFolder.mkdirs();
		if(!g_RecordBufferFolder.exists())g_RecordBufferFolder.mkdirs();
		if(!g_RecordFolder.exists())g_RecordFolder.mkdirs();
		if(!g_ReminderFolder.exists())g_ReminderFolder.mkdirs();
		if(!g_TempPath.exists())g_TempPath.mkdirs();
	}
    /**
     * 获取文件目录下某种类型文件名称
     * @param directory：目录绝对路径
     * @param filter：文件名称过滤器
     * @return 文件名字数组
     */
    public static String[] getFileNames(String directory,String filter){
    	String[] fileNames = null;
    	File diry = new File(directory);
    	if(diry.exists()&&diry.isDirectory()){
    		FilenameFilter ff = new FilenameFilter() {
				@Override
				public boolean accept(File file, String name) {
					//do filter here
					return true;
				}
			};
			fileNames = diry.list(ff);
    	}
    	return fileNames;
    }

    public static String getFileName(int songIndex,String directory, final String filter){
    	String[] fileNames = null;
    	String fileName = null;
    	File diry = new File(directory);
    	if(diry.exists()&&diry.isDirectory()){
//    		FilenameFilter ff = new FilenameFilter() {
//				@Override
//				public boolean accept(File file, String name) {
//					//do filter here
//					if(name.endsWith(filter))
//						return true;
//					else
//						return false;
//				}
//			};
			fileNames = diry.list();
			for (int i = 0; i < fileNames.length; i++) {
				if(i == songIndex)
					fileName = fileNames[i];
			}
    	}
    	return fileName;
    }
    /**
     * 获取文件目录下某种类型的文件总数
     * @param directory：目录绝对路径
     * @param filter：文件名称过滤器
     * @return
     */
	public static long getFileCount(String directory,final String filterStr) {
		int count = 0;
		File folderStr = new File(directory);
		if (folderStr != null && folderStr.isDirectory()) {
			FilenameFilter filter = new FilenameFilter() {
				@Override
				public boolean accept(File file, String fileName) {
					if (fileName.endsWith(filterStr))
						return true;
					else
						return false;
				}
			};
			String[] mp3s = folderStr.list(filter);
			count = mp3s.length;
		}
		return count;
	}
}